package com.threegroup.admin.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.threegroup.admin.dao.DishFlavorEntityMapper;
import com.threegroup.admin.dto.DishFlavorDTO;
import com.threegroup.admin.dto.Flavor;
import com.threegroup.admin.entity.DishFlavorEntity;
import com.threegroup.admin.utils.AssertUtil;
import com.threegroup.common.utils.Result;
import com.threegroup.common.validator.AssertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.awt.datatransfer.FlavorEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DishFlavorService {
    @Resource
    private DishFlavorEntityMapper dishFlavorEntityMapper;

    /**
     * 添加口味
     *
     * @param dishFlavorEntity
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveFlavor(DishFlavorEntity dishFlavorEntity) {
        //校验非空
        checkFlavorParams(dishFlavorEntity);
        //校验逻辑该菜品不能的口味名是唯一的
        AssertUtil.isTrue(dishFlavorEntityMapper.queryFlavorBydishIdAndName(dishFlavorEntity) != null, 303, "该口味已存在");
        //默认参数设置
        dishFlavorEntity.setCreateDate(new Date());
        dishFlavorEntity.setUpdateDate(new Date());
        dishFlavorEntity.setUpdater(dishFlavorEntity.getCreator());
        dishFlavorEntity.setIsDeleted(0);
        dishFlavorEntity.setValue(dishFlavorEntity.getValue());//修改系统匹配值

        //执行添加
        AssertUtil.isTrue(dishFlavorEntityMapper.insertSelective(dishFlavorEntity) != 1, 303, "添加失败");

    }

    private void checkFlavorParams(DishFlavorEntity dishFlavorEntity) {
        AssertUtils.isBlank(dishFlavorEntity.getName(), 302, "口味名称不能为空");
        AssertUtils.isBlank(dishFlavorEntity.getValue(), 302, "口味内容不能为空");
        AssertUtils.isNull(dishFlavorEntity.getCreator(), 302, "创建人不能为空");
        AssertUtils.isNull(dishFlavorEntity.getDishId(), 302, "所属菜品不能为空");


    }

    /**
     * 修改口味
     *
     * @param dishFlavorEntity
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateFlavor(DishFlavorEntity dishFlavorEntity) {
        //校验存在
        AssertUtils.isNull(dishFlavorEntity.getId(), 302, "待修改参数不存在");
        //校验内容参数
        checkFlavorParams(dishFlavorEntity);

        dishFlavorEntity.setValue( dishFlavorEntity.getValue());//修改系统匹配值

        //执行修改
        List<DishFlavorEntity> dishFlavorEntity1 = dishFlavorEntityMapper.selectByPrimaryKey(dishFlavorEntity.getId());
        int size = dishFlavorEntity1.size();
        AssertUtil.isTrue(dishFlavorEntityMapper.updateByPrimaryKeySelective(dishFlavorEntity) != size, 303, "修改失败请重试");


    }

    /**
     * 删除菜品
     *
     * @param ids
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteFlavors(String ids) {
        String[] split = ids.split(",");
        List<Long> longsList = new ArrayList<>();
        for (String s : split) {
            Long id = Long.valueOf(s);
            AssertUtils.isNull(dishFlavorEntityMapper.selectByPrimaryKey(id), 303, "待删除记录不存在");
            longsList.add(id);
        }
        //执行批量删除
        int size = longsList.size();
        AssertUtil.isTrue(dishFlavorEntityMapper.deleteByIdList(longsList) != size, 303, "删除失败");
    }

    public Result<PageInfo<Flavor>> getFlavorList(Integer limit1, Integer page1, String order1, String orderField1) {
        //校验参数
        Integer limit;
        Integer page;
        String order;
        String orderField;
        //校验
        limit = (limit1 != null) ? limit1 : 10;//默认10
        page = (page1 != null) ? page1 : 1;//默认第1页
        order = (StringUtils.isBlank(order1)) ? "asc" : order1;//默认
        orderField = (StringUtils.isBlank(orderField1)) ? "name" : orderField1;//默认排序字段
        //开启分页
        PageHelper.startPage(page,limit);
        //获取list集合
       List<DishFlavorEntity> dishFlavorEntityList =  dishFlavorEntityMapper.queryAll(order,orderField);
        //设置口味信息集合
       List<Flavor> flavorList = new ArrayList<>();
       //遍历获取口味集合
        for (DishFlavorEntity dishFlavorEntity : dishFlavorEntityList) {
            List<String> flavors = new ArrayList<>();
            String value = dishFlavorEntity.getValue();
            String[] split = value.split(",");
            for (String s : split) {
                flavors.add(s);
            }
            //准备新对象
            Flavor flavor = new Flavor();
            flavor.setValue(flavors);//
            flavor.setId(dishFlavorEntity.getId());//
            flavor.setDishId(dishFlavorEntity.getDishId());//
            flavor.setCreator(dishFlavorEntity.getCreator());//
            flavor.setCreateDate(dishFlavorEntity.getCreateDate());//
            flavor.setIsDeleted(dishFlavorEntity.getIsDeleted());//
            flavor.setUpdater(dishFlavorEntity.getUpdater());//
            flavor.setUpdateDate(dishFlavorEntity.getUpdateDate());//
            flavor.setName(dishFlavorEntity.getName());//
            //添加到新的list集合
            flavorList.add(flavor);
        }
        PageInfo<Flavor> dishFlavorEntityPageInfo = new PageInfo<Flavor>(flavorList);
        //设置值
        Result<PageInfo<Flavor>> pageInfoResult = new Result<>();
        pageInfoResult.setData(dishFlavorEntityPageInfo);

        return pageInfoResult;
    }

    public Result<List<Flavor>> queryFlavor(String id) {
        //校验
        AssertUtils.isBlank(id,302,"请输入要查询的信息");
        List<DishFlavorEntity> dishFlavorEntityList = dishFlavorEntityMapper.selectByPrimaryKey(Long.valueOf(id));
        AssertUtils.isNull(dishFlavorEntityList,303,"待查询的信息不存在");
        //设置口味信息集合
        List<Flavor> flavorList = new ArrayList<>();
        //遍历获取口味集合
        for (DishFlavorEntity dishFlavorEntity : dishFlavorEntityList) {
            List<String> flavors = new ArrayList<>();
            String value = dishFlavorEntity.getValue();
            String[] split = value.split(",");
            for (String s : split) {
                flavors.add(s);
            }
            //准备新对象
            Flavor flavor = new Flavor();
            flavor.setValue(flavors);//
            flavor.setId(dishFlavorEntity.getId());//
            flavor.setDishId(dishFlavorEntity.getDishId());//
            flavor.setCreator(dishFlavorEntity.getCreator());//
            flavor.setCreateDate(dishFlavorEntity.getCreateDate());//
            flavor.setIsDeleted(dishFlavorEntity.getIsDeleted());//
            flavor.setUpdater(dishFlavorEntity.getUpdater());//
            flavor.setUpdateDate(dishFlavorEntity.getUpdateDate());//
            flavor.setName(dishFlavorEntity.getName());//
            //添加到新的list集合
            flavorList.add(flavor);
        }

        //封装查询结果
        Result<List<Flavor>> dishFlavorEntityResult = new Result<>();
        dishFlavorEntityResult.setData(flavorList);

        return  dishFlavorEntityResult;
    }
}
